//
//  YWLedgerEquTypeTotalCell.m
//  maintain
//
//  Created by 烟雨云渊 on 2017/4/20.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWLedgerEquTypeTotalCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "YYLabel.h"
#import "YWEquipment.h"
#import "YWEquipmentManager.h"

@interface YWLedgerEquTypeTotalCell()
@property (nonatomic, strong) YYLabel * equTypeNameLabel;
@property (nonatomic, strong) YYLabel * totalCountLabel;

@property (nonatomic, strong) UIView* footLine;
@property (nonatomic, strong) UIView* marginLine1;
@property (nonatomic, strong) UIView* marginLine2;
//报障
@property (nonatomic, strong) UIButton* postTrouble;
//维保
@property (nonatomic, strong) UIButton* maintanceButton;
//详情
@property (nonatomic, strong) UIButton* detailButton;
@end

@implementation YWLedgerEquTypeTotalCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

//- (void)setEquipmentArray:(NSMutableArray *)equipmentArray{
//    _equipmentArray = equipmentArray;
////    YWEquipment * equipment = _equipmentArray.count?_equipmentArray[0]:nil;
////    NSString *equipmentTypeName = [[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:equipment.equipmentTypeId];
////    self.equTypeNameLabel.text = equipmentTypeName.length?equipmentTypeName:nil;
//    
//    NSInteger totalCount = _equipmentArray.count;
//    self.totalCountLabel.text = [NSString stringWithFormat:@"共%d个设备",totalCount];
//}
- (void)setEquipmentInfo:(YWQrcodeEquipmentInfo *)equipmentInfo{
    _equipmentInfo = equipmentInfo;
    NSString *equipmentTypeName = [[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:_equipmentInfo.equipmentTypeId];
    self.equTypeNameLabel.text =  equipmentTypeName.length?equipmentTypeName:nil;
    self.totalCountLabel.text = self.totalCountLabel.text = [NSString stringWithFormat:@"共%d个设备",_equipmentInfo.equipmentCount];
}

- (void)setUpSubviews{
    [self.equTypeNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(10);
        make.left.mas_equalTo(self.contentView).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];
    
    [self.totalCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.equTypeNameLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(self.contentView).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];
    
    [self.footLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.totalCountLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(@1.0);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    [self.postTrouble mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.footLine.mas_bottom).offset(1);
        make.left.mas_equalTo(self.contentView);
        make.width.mas_equalTo(SCREEN_WIDTH/3-1);
        make.bottom.mas_equalTo(self.contentView).offset(-5);
    }];
    
    [self.marginLine1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.postTrouble);
        make.left.mas_equalTo(self.postTrouble.mas_right);
        make.width.mas_equalTo(@1.0);
        make.bottom.mas_equalTo(self.postTrouble);
    }];
    
    [self.maintanceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.postTrouble);
        make.left.mas_equalTo(self.marginLine1.mas_right).offset(1);
        make.width.mas_equalTo(SCREEN_WIDTH/3-1);
        make.bottom.mas_equalTo(self.postTrouble);
    }];
    
    [self.marginLine2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.postTrouble);
        make.left.mas_equalTo(self.maintanceButton.mas_right);
        make.width.mas_equalTo(@1.0);
        make.bottom.mas_equalTo(self.postTrouble);
    }];
    
    [self.detailButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.postTrouble);
        make.left.mas_equalTo(self.marginLine2.mas_right).offset(1);
        make.right.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.postTrouble);
    }];
}

- (YYLabel*)equTypeNameLabel{
    if (!_equTypeNameLabel) {
        YYLabel* buildingLabel = [[YYLabel alloc] init];
        buildingLabel.font = [UIFont boldSystemFontOfSize:16];
        buildingLabel.textColor = [UIColor blackColor];
        _equTypeNameLabel = buildingLabel;
        [self.contentView addSubview:_equTypeNameLabel];
    }
    return _equTypeNameLabel;
}

- (YYLabel*)totalCountLabel{
    if (!_totalCountLabel) {
        YYLabel* totalCountLabel = [[YYLabel alloc] init];
        totalCountLabel.textColor = [UIColor customTextGrayColor];
        totalCountLabel.font = KSmallContentFont;
        _totalCountLabel = totalCountLabel;
        [self.contentView addSubview:_totalCountLabel];
    }
    return _totalCountLabel;
}

-(UIButton *)postTrouble{
    if (!_postTrouble) {
        UIButton* postTrouble = [UIButton buttonWithType:UIButtonTypeSystem];
        [postTrouble setBackgroundColor:[UIColor whiteColor]];
        [postTrouble setTitle:@"报障" forState:UIControlStateNormal];
        postTrouble.titleLabel.font = kContentFont;
        [postTrouble setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [postTrouble addTarget:self action:@selector(handleButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        postTrouble.tag = 1001;
        [self.contentView addSubview:postTrouble];
        _postTrouble = postTrouble;
    }
    return  _postTrouble;
}

-(UIButton *)maintanceButton{
    if (!_maintanceButton) {
        UIButton* maintanceButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [maintanceButton setBackgroundColor:[UIColor whiteColor]];
        [maintanceButton setTitle:@"维保" forState:UIControlStateNormal];
        maintanceButton.titleLabel.font = kContentFont;
        [maintanceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [maintanceButton addTarget:self action:@selector(handleButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        maintanceButton.tag = 1002;
        [self.contentView addSubview:maintanceButton];
        _maintanceButton = maintanceButton;
    }
    return _maintanceButton;
}

-(UIButton *)detailButton{
    if (!_detailButton) {
        UIButton* recordButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [recordButton setBackgroundColor:[UIColor whiteColor]];
        [recordButton setTitle:@"详情" forState:UIControlStateNormal];
        recordButton.titleLabel.font = kContentFont;
        [recordButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [recordButton addTarget:self action:@selector(handleButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        recordButton.tag = 1003;
        [self.contentView addSubview:recordButton];
        _detailButton = recordButton;
    }
    return _detailButton;
}

-(UIView *)footLine{
    if (!_footLine) {
        UIView *footLine = [[UIView alloc]init];
        [footLine setBackgroundColor:[UIColor customLineColor]];
        [self.contentView addSubview:footLine];
        _footLine = footLine;
    }
    return _footLine;
}
-(UIView *)marginLine1{
    if (!_marginLine1) {
        UIView *marginLine1 = [[UIView alloc]init];
        [marginLine1 setBackgroundColor:[UIColor customLineColor]];
        [self.contentView addSubview:marginLine1];
        _marginLine1 = marginLine1;
    }
    return _marginLine1;
}
-(UIView *)marginLine2{
    if (!_marginLine2) {
        UIView *marginLine2 = [[UIView alloc]init];
        [marginLine2 setBackgroundColor:[UIColor customLineColor]];
        [self.contentView addSubview:marginLine2];
        _marginLine2 = marginLine2;
    }
    return _marginLine2;
}

- (void)handleButtonEvent:(UIButton*)sender
{
    if (sender.tag == 1001) {
        if ([self.delegate respondsToSelector:@selector(clickEquTypePostTroubleButtonEvent:)]) {
            [self.delegate clickEquTypePostTroubleButtonEvent:self];
        }
    }else if (sender.tag == 1002) {
        if ([self.delegate respondsToSelector:@selector(clickEquTypeMaintanceButtonEvent:)]) {
            [self.delegate clickEquTypeMaintanceButtonEvent:self];
        }
    }else if (sender.tag == 1003) {
        if ([self.delegate respondsToSelector:@selector(clickEquTypeDetailButtonEvent:)]) {
            [self.delegate clickEquTypeDetailButtonEvent:self];
        }
    }
}

@end
